package wikitools.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import wikitools.model.Article;
import wikitools.model.ModelUtil;

import com.thoughtworks.xstream.XStream;

public class XMLUtil {
    


    public static <T, E> void alias(T obj, XStream xstream) {
        xstream.alias(getSimplestClassName(obj), obj.getClass());
        if(obj instanceof Collection) {
            for(E elem : (Collection<E>)obj) {
                XMLUtil.alias(elem, xstream);                
            }
        }
    }
    
//    public static<T> void alias(Collection<T> obj, XStream xstream) {
//        System.out.println("HOHO");
//        xstream.alias(getSimplestClassName(obj), obj.getClass());
//        for(T elem : obj) {
//            XMLUtil.alias(elem, xstream);
//            
//        }
//    }

    /**
     * Transforms an object in to its xml form
     * 
     * @param <T>
     *            object type
     * @param obj
     *            an object
     * @return xml form string for the object
     */
    public static <T> String toXML(T obj) {
        XStream xstream = new XStream();
        xstream.registerConverter(new RSSDateConverter());
        // simplify class name. e.g. <wikitools.model.Article> -> <article>
        XMLUtil.alias(obj, xstream);
        return xstream.toXML(obj);
    }


    private static String getSimplestClassName(Object item) {
        return item.getClass().getSimpleName().toLowerCase();
    }

    public static void main(String[] args) {

//        System.out.println(XMLUtil.toXML(ModelUtil.SAMPLEARTICLE));

        List<Article> list = new ArrayList<Article>();
        for (int i = 0; i < 10; i++) {
            list.add(ModelUtil.SAMPLEARTICLE.clone());
        }
        

        System.out.println(XMLUtil.toXML(list));
        
        List<String> lists = new ArrayList<String>();
        for (int i=0; i<10; i++) {
            lists.add(Integer.toString(i));
        }
        
        System.out.println(XMLUtil.toXML(lists));
        
        XStream xstream = new XStream();
        // simplify class name. e.g. <wikitools.model.Article> -> <article>
        XMLUtil.alias(lists, xstream);
        System.out.println(xstream.toXML(lists));
    }
    
    /***************************************/

    // public static <T> void alias(T obj, XStream xstream) {
    // xstream.alias(getSimplestClassName(obj), obj.getClass());
    //        
    // //TODO: figure out how generics do overloading
    // if(obj instanceof Collection) {
    // XMLUtil.aliasCollection((Collection)obj, xstream);
    // }
    // }
    //
    //       
    //    
    // public static <T extends Collection<E>, E> void aliasCollection(T obj,
    // XStream xstream) {
    // xstream.alias(getSimplestClassName(obj), obj.getClass());
    // for (E elem : obj) {
    // XMLUtil.alias(elem, xstream);
    //
    // }
    // }
}
